# -*— coding: utf-8 -*-
#
# @Time : 2023/09/17 4:12
# @Author : LeeLU
# @File : demo2.py
# @IDE : PyCharm
# @Email : luxuan888@vip.qq.com
# @Wechat : qianyongbuwan
import pandas as pd
from selenium.common.exceptions import NoSuchElementException
import time
import selenium.common.exceptions
from bs4 import BeautifulSoup
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

url = 'https://www.zhipin.com/web/geek/job?query=%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95&city=101020100'
# driver= webdriver.Edge("C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe")
# driver=webdriver.PhantomJS()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)


def get_Boss():
    driver.get(url)
    try:
        driver.implicitly_wait(5)
        driver.find_element(By.CLASS_NAME, 'boss-login-close').click()
        print('关闭搜索框')
    except NoSuchElementException as e:
        print('未找到弹窗')


    try:
        jobs = []  # 岗位列表，保存所以岗位
        adders = []  # 地址
        moneys = []  # 薪酬
        dems = []  # 要求
        # 页数，可自定义
        for i in range(3):
            # #获取网页源码
            html = driver.page_source
            bs = BeautifulSoup(html, 'html.parser')

            job = bs.find_all('span', {'class': 'job-name'})  # 找到对应属性的span标签，格式为列表
            adder = bs.find_all('span', {'class': 'job-area'})
            money = bs.find_all('span', {'class': 'salary'})
            dem = bs.find_all('div', {'class': 'info-append clearfix'})
            for j in job:
                j = j.get_text()  # 提取标签下text文本
                print(j)
                jobs.append(j)  # 添加到列表
                # f = pd.DataFrame(data={})
            for a in adder:
                a = a.get_text()
                adders.append(a)
                # print(a)
                # f = pd.DataFrame(data={})
            for m in money:
                m = m.get_text()
                moneys.append(m)
                # print(m)
                # f = pd.DataFrame(data={})
            for d in dem:
                d = d.get_text()
                dems.append(d)
                # print(d)

            # 点击下一页
            btn = wait.until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, '#main > div > div.job-list > div.page > a.next')))
            driver.execute_script('arguments[0].click();', btn)
            time.sleep(10)
        # 文件保存
        f = pd.DataFrame(data={'岗位': jobs, '地址': adders, '薪酬': moneys, '要求': dems})
        f.to_csv('Boss直聘招聘信息.csv', encoding='utf-8-sig')
    except selenium.common.exceptions.NoSuchElementException:
        print('运行出错了！')


if __name__ == '__main__':
    get_Boss()
